<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/includes/date.class.php');
    require_once($config['app_dir'] . '/classes/stockplot.data.php');
    //---------------------------------------------------------------------------------------------------
    class objSparkLineParams extends objStockParams implements iErrorHandler, iDebug{

        public function __construct() {
            if ( false === $this->_getTrade() ) return ;
            $this->_getStart();
            $this->_getFinish();
            $this->_getT();
        } //constructor

        public function ClassName() {
            return  __CLASS__;
        }//ClassName
        //----------------------------------------------------------------------
        protected function _getStart() {
        try {
            parent::_getStart();
        } catch (Exception $e) { }
        } //getStart

        protected function _getFinish() {
            try {
                parent::_getFinish();
            } catch (Exception $e) { }
        } //getFinish

    } //class objStockParams

    //-------------------------------------------------------------------------------------------------------
    class objSparkLineModel extends objStockModel implements iErrorHandler{
            public $band_width = 1;

        public function __construct($trade, $start=false, $finish=false, $system=false) {
            $this->_trade = $trade;
            $this->_start = $start;
            $this->_finish = $finish;
            if ( !$this->_trade ) {

                throw new Exception($this->Error(1));
            }

            $this->_db = objDataBase::GetInstance();
            $this->system_id = $system;
            $this->_sqls();
        } //constructor

        public function ClassName() {
            return  __CLASS__;
        }//ClassName

        protected function _Scaling() {
            $this->_scale['x']['min'] = 0;
            $this->_scale['x']['max'] = count($this->labels['x'])+1;
            $this->_scale['y']['min'] = min(array_merge($this->vectors['y'], $this->stocks['y']));
            $this->_scale['y']['max'] = max(array_merge($this->vectors['y'], $this->stocks['y']));
        } //_Scaling

        protected function  _TranslateStopMarks() {
            return true;
        } //_TranslateStopMarks

        protected function _Stops() {
        }//_Stops

        protected function _Labels() {
            $md=''; $hm='';
            $result=array();
            $borders=array(); $iborder=0; $first=true;
            foreach($this->labels['x'] as $label) {
                $tmd=DateConvert::Date('W', $label);//adodb_date('W', $label);
                if( $tmd!==$md ) {
                    $md=$tmd;
                    if( !$first ) {
                        $borders[]=$iborder;
                    } else $first = false;
                }
                ++$iborder;
            }
            $borders[]=$iborder;
            //------------------------------------------------------------------

            $i=0;
            $major = array();
            while( $i<count($borders) ) {
                $result[$borders[$i]] = $borders[$i];
                $major[] = $borders[$i];
                ++$i;
            }

            $this->labels['xmajor'] = $major;
            $this->labels['xlines'] = $borders;//$count_lines;
        } //_Labels

        protected function _Colors() {
            parent::_Colors();
            //------------------------------------------------------------------
            // check unfilled last day
            //------------------------------------------------------------------
            $forecasts = $this->_data['forecast'];
            $max_stock_time = $this->_db->handler->GetOne($this->_sql_querys['max_time_quote']);
            $colors = $this->vectors['colors'];
            $flags = $this->vectors['flag_colors'];
            for($i=0; $i<count($forecasts); $i++)
                if( !$flags[$i] && (2==$colors[$i] || 4==$colors[$i]) && $forecasts[$i]['end']['time']>=$max_stock_time) {
                    $colors[$i]=3;
                    $this->flag_new_forecast = true;
                }
            $this->vectors['colors'] = $colors;
            unset($colors);
            unset($flags);
        } //_Colours

        protected function _sqls() {
            parent::_sqls();
            //------------------------------------------------------------------
            $this->_sql_querys['quotes'] =
                " SELECT ".
                       " t.Pair, ".
                       " q.nTradeID as TradeID, ".
                       " q.nTimeStart as TimeStart, ".
                       " qo.O as O, ".
                       " q.nH as H, ".
                       " q.nL as L, ".
                       " qc.C as C ".
                " FROM ".
                " ( ".
                  " SELECT  ".
                       " tq.TradeID as nTradeID, ".
                       " MIN(tq.TimeStart) as nTimeStart, ".
                       " MIN(FROM_UNIXTIME(tq.TimeStart)) as TimeStart, ".
                       " MAX(FROM_UNIXTIME(tq.TimeStart)) as TimeFinish, ".
                       " MAX(tq.H) as nH, ".
                       " MIN(tq.L) as nL ".
                  " FROM fa_quotes tq ".
                  " GROUP BY tq.TradeID, date_format(FROM_UNIXTIME(tq.TimeStart), '%Y%m%d') ".
                " ) AS q ".
                " LEFT OUTER JOIN fa_quotes qc ON (qc.TradeID = q.nTradeID and FROM_UNIXTIME(qc.TimeStart)=q.TimeFinish) ".
                " LEFT OUTER JOIN fa_quotes qo ON (qo.TradeID = q.nTradeID and FROM_UNIXTIME(qo.TimeStart)=q.TimeStart) ".
                " LEFT OUTER JOIN fa_trades t ON (t.TradeID = q.nTradeID) ".
                " WHERE q.nTradeId=$this->_trade AND q.nTimeStart between UNIX_TIMESTAMP('$this->_start') and UNIX_TIMESTAMP('$this->_finish') " ;

            //------------------------------------------------------------------
            $where_systemid = '';
            if( $this->system_id ) $where_systemid = ' AND f.SystemID = ' . $this->system_id. ' ';
            //------------------------------------------------------------------

            $this->_sql_querys['min_time_forecast'] =
                    "SELECT min(f.BeginTime) as MinTime FROM vfa_forecast f WHERE f.TradeID=$this->_trade". $where_systemid;
            //------------------------------------------------------------------
            $this->_sql_querys['max_time_quote'] =
                //"SELECT max(q.TimeStart) as MaxTime FROM fa_quotes q WHERE q.TradeID=$this->_trade and q.TimeStart<=UNIX_TIMESTAMP()";
                "SELECT max(q.TimeStart) as MaxTime FROM fa_quotes q WHERE q.TradeID=$this->_trade AND  q.TimeStart between UNIX_TIMESTAMP('$this->_start') and UNIX_TIMESTAMP('$this->_finish') ";
        } //_sqls
    } //class objSparkLineModel
//-------------------------------------------------------------------------------------------------------
?>
